var express = require('express');
var router = express.Router();

//引入连接数据库模块
const conn = require('../conn')

//连接数据
conn.connect(() => {

})
var jsonWrite = function(res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
            code: '1',
            msg: '操作失败'
        });
    } else {
        res.json(
            ret
        );
    }
};
//轮播图
router.get('/shopSwiper', function(req, res) {
    const sqlStr = 'select * from carousel'
    conn.query(sqlStr, (err, result) => {
        if (err) {
            throw err;
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});
//瀑布流
router.get('/shopWaterpull', function(req, res) {
    let category_id = req.query.category_id
    const sqlStr = `select * from shop where category_id='${category_id}'`
    conn.query(sqlStr, (err, result) => {
        if (err) {
            throw err;
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});
// 商品详情
router.get('/shopDetail', function(req, res) {
    let product_id = req.query.product_id
    const sqlStr = `select * from shop where  product_id='${product_id}'`
    conn.query(sqlStr, (err, result) => {
        if (err) {
            throw err;
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});

//左侧分类
router.get('/category', function(req, res) {
    const sqlStr = 'select * from category'
    conn.query(sqlStr, (err, result) => {
        if (err) {
            throw err;
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});
//右侧分类
router.get('/shopCategory', function(req, res) {
    let category_id = req.query.category_id
    const sqlStr = `select * from shop where category_id='${category_id}'`
    conn.query(sqlStr, (err, result) => {
        if (err) {
            throw err;
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});
//添加购物车
router.post('/shopCart', function(req, res) {
    const sqlStr = 'insert into shoppingcart(product_name,price,product_id,num,image,userId) values (?,?,?,?,?,?)';
    var params = req.body
    conn.query(sqlStr, [params.product_name, params.price, params.product_id, params.num, params.image, params.userId], (err, result) => {
        if (err) {
            throw err;
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});
//购物车
router.post('/shopCartDeatil', function(req, res) {
    let { userId } = req.body
    const sqlStr = `select * from shoppingcart where userId='${userId}'`;
    conn.query(sqlStr, (err, result) => {
        if (err) {
            throw err;
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});
//购物车删除
router.post('/DelshopCart', function(req, res) {
    let { id } = req.body
    const sqlStr = `delete  from shoppingcart where  id='${id}'`;
    conn.query(sqlStr, (err, result) => {
        if (err) {
            throw err;
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});
//搜索 模糊查询
router.post('/searchGoods', (req, res) => {
    let { product_name } = req.body;
    const sqlStr = `select * from shop where  product_name like '%${product_name}%'`
    conn.query(sqlStr, (err, data) => {
        if (err) {
            throw err;
        } else {
            res.send(data)
        }
    })
});



module.exports = router;